import { client } from '../../request/index.js'

Page({
  data: {
    imgIp: getApp().globalData.imgIp,
    ifError: null,
    isLoading: true,
    err_result: '',
    err_reason: '',
    love: '/1606032387067.png',
    reward: '/1606032485742.png',
    loveNumber: 1,
    rewardNumber: 0,
    loveNumberHidden: true,
    rewardNumberHidden: true,
    isShowImage: false,
    width: wx.getSystemInfoSync().windowWidth,
    height: wx.getSystemInfoSync().windowHeight,
    tabbar: null,
  },
  openLove() {
    this.setData({
      love: '/1606062189177.png',
      loveNumberHidden: false,
    })
  },

  openReward() {
    this.setData({
      reward: '/1606062240475.png',
      rewardNumberHidden: false,
    })
  },
  loadTempImagePath(options) {
    let that = this
    wx.getSystemInfo({
      success(res) {
        var image_x = 15
        var image_y = 100
        var image_width = that.data.width - 2 * 15
        var image_height = 365
        wx.getImageInfo({
          src: options,
          success(res) {
            that.setData({
              isShowImage: true,
            })
            that.canvas = wx.createCanvasContext('image-canvas', that)
            that.canvas.drawImage(
              options,
              0,
              0,
              that.data.width,
              that.data.height
            )
            that.canvas.setStrokeStyle('black')
            that.canvas.strokeRect(image_x, image_y, image_width, image_height)
            that.canvas.draw()
            setTimeout(function () {
              wx.canvasToTempFilePath({
                canvasId: 'image-canvas',
                x: image_x, //画布x轴起点
                y: image_y, //画布y轴起点
                width: image_width, //画布宽度
                height: image_height, //画布高度
                destWidth: image_width, //输出图片宽度
                destHeight: image_height, //输出图片高度
                success: function (res) {
                  let image = res.tempFilePath
                  // 在此可进行网络请求
                  wx.uploadFile({
                    url: getApp().globalData.ip + '/py/image',
                    filePath: image,
                    name: 'filePath',
                    method: 'POST',
                    formData: {},
                    header: {
                      cookie: 'JSESSIONID=' + wx.getStorageSync('sessionid'),
                      'content-type': 'application/x-www-form-urlencoded',
                      'content-type': 'multipart/form-data',
                    },
                    dataType: 'json',
                    success: function (res) {
                      console.log('识别结果:', res)
                      let flag = res.data[1]
                      console.log('feikongpan:', flag) // 1空盘0非空盘
                      if (flag === '1') {
                        that.red()
                      } else {
                        that.setData({
                          isLoading: false,
                          ifError: true,
                          err_result: '识别失败',
                          err_reason: 'AI可能还不认识这样的光盘，请再试一次',
                        })
                      }
                    },
                    fail: function (res) {
                      console.log('连接失败:', res)
                      that.setData({
                        isLoading: false,
                        ifError: true,
                        err_result: '服务器连接失败',
                        err_reason: '上传失败，请检查网络是否连通哦',
                      })
                    },
                    complete() {},
                  })
                },
                fail: function (e) {
                  that.setData({
                    isLoading: false,
                    ifError: true,
                    err_result: '照片处理失败',
                    err_reason: '请联系客服QAQ',
                  })
                },
              })
            }, 1000)
          },
        })
      },
    })
  },
  //识别成功返回光币数
  async red() {
    let that = this
    const mapData = await client({
      url: '/compare',
      method: 'POST',
    })
    console.log('成功奖励:', mapData)
    if (mapData.data.code === 888) {
      that.setData({
        isLoading: false,
        ifError: true,
        err_result: '打卡失败',
        err_reason: mapData.data.message,
      })
      return
    }
    let rewardNumber = mapData.data.data
    getApp().globalData.rewardNumber = rewardNumber
    // 第一次成功引导
    let tmp = wx.getStorageSync('successGuideOpen')
    // 判断出是不是第一次
    if (tmp === undefined || tmp === '') {
      wx.setStorage({
        key: 'successGuideOpen',
        data: true,
      })
    }
    wx.switchTab({
      url: '../../pages/homepage/homepage',
    })
  },
  toTabbar() {
    if (this.data.tabbar === '0') {
      wx.switchTab({
        url: '../../pages/homepage/homepage',
      })
    } else {
      wx.switchTab({
        url: '../../pages/photo/photo',
      })
    }
  },
  // 打卡失败时再次跳转打卡
  toTabbarErr() {
    if (this.data.tabbar === '0') {
      wx.redirectTo({
        url: '/pages/photo_h/photo_h',
      })
    } else {
      wx.switchTab({
        url: '../../pages/photo/photo',
      })
    }
  },

  onLoad: function (options) {
    console.log(options)
    this.loadTempImagePath(options.img_path)
    this.setData({
      tabbar: options.tabbar,
    })
  },
})
